//按键操作

#include "App_Main.h"
#include "Keyboard.h"

_key_param key_param;
uint8_t key_ONOFF;
uint8_t key_mset_state = 0; //m Set值,[0,2],默认0
uint16_t Keyboard_Show_Count;
uint16_t Keyboard1_Show_Count;
uint16_t pro_Show_Count;
uint16_t PR_temp;
uint8_t pro_flash; //闪烁灭控抽状态

//通过示波器测量按键延迟, 15ms: 计数: 2500 * 16
void keyboard_delay15ms(void)
{
//	uint32_t i;

delay_nms(15);

//	for (i = 0; i < 2500 * 200; i++)
//	{
//	}
}

void Key_init(void)
{
	key_param.once1 = 0;
	key_param.once2 = 0;
	key_param.once3 = 0;
	key_param.state_keyled = 0;
	GPIO_Init(GPIOA0,IO_Key1,Intput);
	GPIO_Init(GPIOA0,IO_Key2,Intput);

	Keyboard_Show_Count = 0;
	Keyboard1_Show_Count = 0;
}

//Tick 2ms
void Key_Clock(void)
{
	if(FactoryEnable > 0) return;
	if (Keyboard_Show_Count > 0) Keyboard_Show_Count--;
	if (Keyboard1_Show_Count > 0)
	{
		Keyboard1_Show_Count--;
		if (Keyboard1_Show_Count == 5)
		{
			Keyboard1_Show_Count = 0;
			if(FactoryEnable == 0)
			{ //非出厂测试,闪耀有效
				pro_Show_Count = 1500; //3000;
				NT_Shine_Rest();
			}
		}
	}
}

//Key1,Key2,Key3
void KeyDetection(void)
{
	uint16_t temp;

	//key2
	if (ReadIO_Key2 == 0)
	{
		keyboard_delay15ms();
		if ((ReadIO_Key2 == 0) && (key_param.once2 == 0) && (Keyboard1_Show_Count == 0))
		{
			key_param.once2 = 1;

			if((Keyboard1_Show_Count == 0) && (FactoryEnable == 0))
			{
				if(Switch_State == Temp_Set_Smoke)
				{
					if(Keyboard_Show_Count > 0)
					{
						UI_System.Pset++;
						if (UI_System.Pset > PSet_7) UI_System.Pset = PSet_0;
						Smoke_P_Value = UI_System.Pset;
		                Flash_Write();
					}
					Keyboard_Show_Count = 1000;
				}
			}
		}
	}
	else if (ReadIO_Key2 > 0)
	{
		keyboard_delay15ms();
		if (key_param.once2 > 0)
		{
			key_param.once2 = 0;
		}
	}
	if (ReadIO_Key1 == 0)
	{
		keyboard_delay15ms();
		if ((ReadIO_Key1 == 0) && (key_param.once1 == 0) && (Keyboard_Show_Count == 0))
		{
			key_param.once1 = 1;
			if(key_mset_state == 0)
			{
				if(RTD[1].status == Pt_Normal)
				{
					key_mset_state = 1;
					if(FactoryEnable == 0) Keyboard1_Show_Count = 1000;
				}
				else if(RTD[2].status == Pt_Normal)
				{
					key_mset_state = 2;
					if(FactoryEnable == 0) Keyboard1_Show_Count = 1000;
				}
			}
			else if(key_mset_state == 1)
			{
				if(RTD[2].status == Pt_Normal)
				{
					key_mset_state = 2;
					if(FactoryEnable == 0) Keyboard1_Show_Count = 1000;
				}
			}
			else if(key_mset_state == 2)
			{
				if(RTD[1].status == Pt_Normal)
				{
					key_mset_state = 1;
					if(FactoryEnable == 0) Keyboard1_Show_Count = 1000;
				}
			}
		}
	}
	else if (ReadIO_Key1 > 0)
	{
		keyboard_delay15ms();
		if (key_param.once1 > 0)
		{
			key_param.once1 = 0;
		}
	}
}
